
package org.openeai.lite.esb.transformers;

import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.mule.api.transformer.TransformerException;
import org.mule.config.i18n.CoreMessages;
import org.mule.transformer.AbstractTransformer;

public class StringToPayloadMap extends AbstractTransformer
{

    private Logger log = Logger.getLogger(this.getClass());

    private static String errMsg = "Payload is NOT of expected type java.lang.String";

    @SuppressWarnings("deprecation")
    public StringToPayloadMap()
    {
        registerSourceType(String.class);
        setReturnClass(java.util.Map.class);
    }

    @Override
    protected Object doTransform(Object payload, String encoding) throws TransformerException
    {

        try
        {
            log.info("Received payload.");
            if (payload instanceof String)
            {
                log.info("Payload is of expected type java.lang.String");

                log.info("Storing payload value (" + payload.toString() + ") in map.");
                Map<String, String> payloadMap = new HashMap<String, String>();
                payloadMap.put("payload", payload.toString());

                log.info("Transformer complete. Returning map.");
                return payloadMap;
            }
            else
            {
                log.error(errMsg);
                throw new Exception(errMsg);
            }
        }
        catch (Exception e)
        {
            throw new TransformerException(CoreMessages.transformFailed("String", "Map"), e);
        }

    }
}
